package com.iafme.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.ProjectReqDTO;
import com.iafme.dto.res.ProjectResDTO;
import com.iafme.model.Project;
import com.iafme.mapper.ProjectMapper;
import com.iafme.service.IProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author summer
 * @since 2025/01/05 21:14:44
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    @Override
    public Page<ProjectResDTO> lists(ProjectReqDTO projectReqDTO) {
        Page<Project> page=new Page<>(projectReqDTO.getPageNum(),projectReqDTO.getPageSize());
        LambdaQueryWrapper<Project> lambdaQueryWrapper = Wrappers.lambdaQuery(Project.class).like(StrUtil.isNotEmpty(projectReqDTO.getName()), Project::getName, projectReqDTO.getName())
                .orderByDesc(Project::getCreateTime);
        page=this.page(page, lambdaQueryWrapper);
        return Convert.convert(new TypeReference<Page<ProjectResDTO>>() {},page);
    }
}
